#!/usr/bin/env groovy
package com.yidao.www


def wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, buildResult){
    if(webhookUrl == ''){
        return null
    }
    sh """
        curl ${webhookUrl} \
               -H 'Content-Type: application/json' \
               -d '
               {
                    "msgtype":"markdown",
                    "markdown":{
                        "content":"**【${jobName}】** 第 ${buildNumber} 轮 构建<font color='info'> ${buildResult} </font>
                        \n>Jenkins地址:[查看构建任务](${jobUrl})\n>日志地址:[查看构建日志](${logUrl})
                        \n~一个有温度的机器人~"
                    }
               }'
    """
    if(username != ''){
        sh """
            curl ${webhookUrl} \
                -H 'Content-Type: application/json' \
                -d '
                {
                    "msgtype": "text",
                    "text": {
                        "mentioned_list":["${username}"]
                    }
                }'
        """
    }
}


def commitTemplateNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, message){
    if(webhookUrl == ''){
        return null
    }
    sh """
        curl ${webhookUrl} \
               -H 'Content-Type: application/json' \
               -d '
               {
                    "msgtype":"markdown",
                    "markdown":{
                        "content":"**【${jobName}】** 第 ${buildNumber} 轮 构建<font color='info'> ${message} </font>
                        \n>Jenkins地址:[查看构建任务](${jobUrl})\n>日志地址:[查看构建日志](${logUrl})
                        \n~一个有温度的机器人~"
                    }
               }'
    """
    if(username != ''){
        sh """
            curl ${webhookUrl} \
                -H 'Content-Type: application/json' \
                -d '
                {
                    "msgtype": "text",
                    "text": {
                        "mentioned_list":["${username}"]
                    }
                }'
        """
    }
}

def emailNotification(jobName, buildNumber, recipients, buildResult){
    emailext subject: "${jobName} - Build # ${buildNumber} - ${buildResult}",
        body: '''
            ${SCRIPT,template="groovy-html-yidao.template"}
        ''',
        to: "${recipients}"
}


def timeoutNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, username, buildResult){
    if(webhookUrl == ''){
        return null
    }
    sh """
        curl ${webhookUrl} \
               -H 'Content-Type: application/json' \
               -d '
               {
                    "msgtype":"markdown",
                    "markdown":{
                        "content":"**【${jobName}】** 第 ${buildNumber} 轮 构建<font color='info'> 超时 </font>
                        \n>Jenkins地址:[查看构建任务](${jobUrl})\n>日志地址:[查看构建日志](${logUrl})
                    }
               }'
    """
    if(username != ''){
        sh """
            curl ${webhookUrl} \
                -H 'Content-Type: application/json' \
                -d '
                {
                    "msgtype": "text",
                    "text": {
                        "mentioned_list":["${username}"]
                    }
                }'
        """
    }
}